// pages/myMember/myOrders/index.js
var sliderWidth = 68; // 需要设置slider的宽度，用于计算中间位置
var network = require("../../../utils/network.js")
Page({
  data: {
    tabs: ["已完成", "未完成", "待支付"],
    src:'http://housecleanimags-1255598775.cosbj.myqcloud.com/master/taobao.png',
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
  },
  changflag:function(e){
    var index = e.currentTarget.dataset.id
    var master =this.data.master
    var flag = master[index].flag
    master[index].flag = !flag
    this.setData({
      master: master
    })

  },
  offOrder:function(e){
    var that = this 
    var index = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '取消订单会扣除您20元定金其余返还',
      success: function (res) {
        if (res.confirm) {
          var nopayOrder = that.data.master
          nopayOrder[index].flag2 = true
          that.setData({
            master1: nopayOrder
          })
          console.log(that.data.master)
          // 取消订单
          // network.postData(
          // )
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  payOrder: function (e) {
    var that = this 
    var index = e.currentTarget.dataset.id
    var payOrder = that.data.master2
    payOrder[index].hidden = false
    that.setData({
      master2: payOrder,
      payindex:index
    }) 
  },
  pay: function () {
    var that = this
    var data = {}
    data.openid = wx.getStorageSync("openid")
    data.uid = wx.getStorageSync("uid")
    data.token = wx.getStorageSync("token")
    data.orderId = that.data.orderId
    network.postData(
      "https://api.hsb1234.com/userc/v1/pay",
      data,
      function (res) {
        var resdata = res.data.data
        console.log(resdata)
        wx.requestPayment({
          'timeStamp': resdata.timeStamp,
          'nonceStr': resdata.nonceStr,
          'package': resdata.package,
          'signType': resdata.signType,
          'paySign': resdata.sign,
          'success': function (res) {
            wx.redirectTo({
              url: '../../myMember/myOrder/index',
            })
          },
          'fail': function (res) {
            console.log()
          }
        })
      },
      function () {

      }
    )
  },
  cancel: function () {
    var index = this.data.payindex
    var paymaster= this.data.master2
    paymaster[index].hidden = true
    this.setData({
      master2: paymaster
    })
  },
  onLoad: function () {
    var that = this;
    var data ={}
    data.openid = wx.getStorageSync("openid")
    data.uid = wx.getStorageSync("uid")
    data.token = wx.getStorageSync("token")
    data.offset = "0"
    data.limit = "10"
    data.stutas = "已完成"
    network.getData(
      "https://api.hsb1234.com/userc/v1/orders",
      data,
      function(res){
        var resdata = res.data.data
        var master=[]
        for(var i = 0 ; i <resdata.length; i++){
          master[i]={}
          master[i].id = i
          master[i].flag = true
          master[i].orderId = resdata[i].orderId
          master[i].duration = resdata[i].duration/60
          master[i].serviceTime = resdata[i].serviceTime
          master[i].serviceAddress = resdata[i].serviceAddress
          master[i].orderTime = resdata[i].orderTime
          master[i].name = resdata[i].cleaner.name
          master[i].avatar = resdata[i].cleaner.avatar
          master[i].start = resdata[i].cleaner.start
          master[i].phone = resdata[i].cleaner.phone
          master[i].number = resdata[i].cleaner.number
          var star = "★"       
          var starword = []
          for (var j = 0; j < resdata[i].cleaner.start; j++) {
            starword.push(star)
          }
          master[i].starword = starword.join(" ")
        }
        that.setData({
          master:master
        })
      },
      function(){}
    )
    // var data1 = {}
    // data1.openid = wx.getStorageSync("openid")
    // data1.uid = wx.getStorageSync("uid")
    // data1.token = wx.getStorageSync("token")
    // data1.offset = "0"
    // data1.limit = "10"
    // data1.stutas = "未完成"
    // network.getData(
    //   "https://api.hsb1234.com/userc/v1/orders",
    //   data1,
    //   function (res) {
    //     var resdata = res.data.data
    //     var master = []
    //     for (var i = 0; i < resdata.length; i++) {
    //       master[i] = {}
    //       master[i].id = i
    //       master[i].flag = true
    //       master[i].orderId = resdata[i].orderId
    //       master[i].duration = resdata[i].duration / 60
    //       master[i].serviceTime = resdata[i].serviceTime
    //       master[i].serviceAddress = resdata[i].serviceAddress
    //       master[i].orderTime = resdata[i].orderTime
    //       master[i].name = resdata[i].cleaner.name
    //       master[i].avatar = resdata[i].cleaner.avatar
    //       master[i].start = resdata[i].cleaner.start
    //       master[i].phone = resdata[i].cleaner.phone
    //       master[i].number = resdata[i].cleaner.number
    //       var star = "★"
    //       var starword = []
    //       for (var j = 0; j < resdata[i].cleaner.start; j++) {
    //         starword.push(star)
    //       }
    //       master[i].starword = starword
    //     }
    //     that.setData({
    //       master1: master
    //     })
    //   },
    //   function () { }
    // )
    // var data2 = {}
    // data2.openid = wx.getStorageSync("openid")
    // data2.uid = wx.getStorageSync("uid")
    // data2.token = wx.getStorageSync("token")
    // data2.offset = "0"
    // data2.limit = "10"
    // data2.stutas = "待支付"
    // network.getData(
    //   "https://api.hsb1234.com/userc/v1/orders",
    //   data,
    //   function (res) {
    //     var resdata = res.data.data
    //     var master = []
    //     for (var i = 0; i < resdata.length; i++) {
    //       master[i] = {}
    //       master[i].id = i
    //       master[i].flag = true
    //       master[i].hidden = true
    //       master[i].orderId = resdata[i].orderId
    //       master[i].duration = resdata[i].duration / 60
    //       master[i].serviceTime = resdata[i].serviceTime
    //       master[i].serviceAddress = resdata[i].serviceAddress
    //       master[i].orderTime = resdata[i].orderTime
    //       master[i].name = resdata[i].cleaner.name
    //       master[i].avatar = resdata[i].cleaner.avatar
    //       master[i].start = resdata[i].cleaner.start
    //       master[i].phone = resdata[i].cleaner.phone
    //       master[i].number = resdata[i].cleaner.number
    //       var star = "★"
    //       var starword = []
    //       for (var j = 0; j < resdata[i].cleaner.start; j++) {
    //         starword.push(star)
    //       }
    //       master[i].starword = starword
    //     }
    //     that.setData({
    //       master2: master
    //     })
    //   },
    //   function () { }
    // )

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  goToEstimate:function(){
    wx.navigateTo({
      url: '../goToEstimate/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})